package com.sky.service.impl;

import com.sky.mapper.OrderMapper;
import com.sky.service.ReportService;
import com.sky.utils.DateUtil;
import com.sky.vo.TurnoverReportVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    OrderMapper orderMapper;

    /**
     * 营业额统计接口
     *
     * @return
     */
    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {
        //1.获取范围日期每一天
        List<LocalDate> dateList = DateUtil.getRangeDate(begin, end);
        //2.根据每一天去查询对应的营业额
        List<Double> amountList = new ArrayList<>();
        dateList.forEach(
                date -> {
                    //获取目标日期的营业额
                    Double amount = orderMapper.turnoverStatistics(date);
                    if (amount == null) {
                        amount = 0.0;
                    }
                    amountList.add(amount);
                }
        );
        //3.构造返回值
        String dateStr = StringUtils.join(dateList, ",");
        String amountStr = StringUtils.join(amountList, ",");

        return TurnoverReportVO.builder()
                .dateList(dateStr)
                .turnoverList(amountStr)
                .build();
    }
}
